﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ServerNodeLib.DataAdapter
{
	/// <summary>
	/// I'm defining an inteface of methods for RoundRobin code to
	/// talk to the database.  It is expected that the conrete
	/// implementations actually construct the returned objects.
	/// </summary>
	public interface INodeDataAdapter
	{
		#region node
		/// <summary>
		/// Returns the node with the given guid or null if not found
		/// </summary>
		/// <param name="nodeId">The given guid to find</param>
		/// <returns></returns>
		Node GetNode(Guid nodeId);
		/// <summary>
		/// Returns every node with the given state
		/// </summary>
		/// <param name="state">The state to filter</param>
		/// <returns></returns>
		List<Node> GetNodes(NodeState state);
		/// <summary>
		/// Returns the number of nodes that satisfy the state
		/// </summary>
		/// <param name="state">The given state</param>
		/// <returns></returns>
		int NumberNodes(NodeState state);
		/// <summary>
		/// Returns the total number of nodes in the system
		/// </summary>
		/// <returns></returns>
		int NumberNodes();
		/// <summary>
		/// Returns every node that has a batch whose start time is between
		/// the min and max inclusive.
		/// </summary>
		/// <param name="maxStartTime">The min start time to return</param>
		/// <param name="minStartTime">The max start time to return</param>
		/// <returns></returns>
		List<Node> GetNodes(DateTime minStartTime, DateTime maxStartTime);
		/// <summary>
		/// Returns every node seen not more recent than lastSeen
		/// </summary>
		/// <param name="minLastSeen">The maximum last seen value to return</param>
		/// <returns></returns>
		List<Node> GetNodes(DateTime maxLastSeen);
		/// <summary>
		/// Saves the node to the database, overrwiting any existing node
		/// with the same guid
		/// </summary>
		/// <param name="node">The node to save</param>
		/// <returns></returns>
		bool SaveNode(Node node);
		/// <summary>
		/// Deletes the node from the database with the given guid
		/// </summary>
		/// <param name="nodeId">The node guid to delete</param>
		/// <returns></returns>
		bool DeleteNode(Guid nodeId);
		/// <summary>
		/// Returns the list of action requests for the given node
		/// </summary>
		/// <param name="nodeId">The given node</param>
		/// <returns></returns>
		List<ActionRequest> LoadActionRequests(Guid nodeId);
		#endregion
	}
	public interface IBatchDataAdapter
	{
		#region batch
		/// <summary>
		/// Returns the batch with the given guid
		/// </summary>
		/// <param name="batchId">The batch id</param>
		/// <returns></returns>
		Batch GetBatch(Guid batchId);
		/// <summary>
		/// Returns the batch running on the given node
		/// </summary>
		/// <param name="nodeId">The node to check</param>
		/// <returns></returns>
		Batch GetBatchOnNode(Guid nodeId);
		/// <summary>
		/// Returns the batches with the requested status
		/// </summary>
		/// <param name="status">The given status</param>
		/// <returns></returns>
		List<Batch> GetBatches(BatchStatus status);
		/// <summary>
		/// Returns the batches with the request status.
		/// The number of returned batches is limited by the limit argument
		/// </summary>
		/// <param name="status">The given status</param>
		/// <param name="limit">The max number of batches to return</param>
		/// <returns></returns>
		List<Batch> GetBatches(BatchStatus status, int limit);
		/// <summary>
		/// Returns the number of batches that match the requested status
		/// </summary>
		/// <param name="status">The given status</param>
		/// <returns></returns>
		int NumberBatches(BatchStatus status);
		/// <summary>
		/// Returns the total number of batches
		/// </summary>
		/// <returns></returns>
		int NumberBatches();
		/// <summary>
		/// Returns the batches in a particular group
		/// </summary>
		/// <param name="groupId">The group id</param>
		/// <returns></returns>
		List<Batch> GetBatchesInGroup(Guid groupId);
		/// <summary>
		/// Stores the batch to the database, overwriting
		/// any existing batch with the same guid
		/// </summary>
		/// <param name="batch">The batch object to save</param>
		/// <returns></returns>
		bool SaveBatch(Batch batch);
		/// <summary>
		/// Removes the batch from the database
		/// </summary>
		/// <param name="batchId">The batch guid to remove</param>
		/// <returns></returns>
		bool DeleteBatch(Guid batchId);
		/// <summary>
		/// Returns the batch resource associated with the batch
		/// and having the client name
		/// </summary>
		/// <param name="BatchId">The batch guid</param>
		/// <param name="clientName">The client path of the resource</param>
		/// <returns></returns>
		BatchResource GetBatchResource(Guid BatchId, String clientName);

		List<BatchCommand> LoadBatchCommands(Guid BatchId);
		List<BatchResource> LoadBatchResources(Guid BatchId);
		List<BatchResource> LoadReturnResources(Guid BatchId);

		#endregion
	}

	public interface IBatchGroupDataAdapter
	{
		#region batchgroup
		BatchGroup GetBatchGroup(Guid GroupId);

		bool SaveBatchGroup(BatchGroup group);
		bool DeleteBatchGroup(Guid groupId);
		#endregion
	}

	// The following delegates are for lazy loading
	public delegate List<BatchCommand> LoadBatchCommands(Guid BatchId);
	public delegate List<BatchResource> LoadBatchResources(Guid BatchId);
	public delegate List<BatchResource> LoadReturnResources(Guid BatchId);

	public delegate List<ActionRequest> LoadActionRequests(Guid NodeId);
}
